<?php

/**
 * Camioneta form base class.
 *
 * @method Camioneta getObject() Returns the current form's model object
 *
 * @package    cronos-doctrine
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseCamionetaForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                  => new sfWidgetFormInputHidden(),
      'empresa_id'          => new sfWidgetFormInputText(),
      'area_id'             => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Area'), 'add_empty' => false)),
      'operador_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Operador'), 'add_empty' => true)),
      'ayudante_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Ayudante'), 'add_empty' => true)),
      'nombre'              => new sfWidgetFormInputText(),
      'placa'               => new sfWidgetFormInputText(),
      'marca'               => new sfWidgetFormInputText(),
      'modelo'              => new sfWidgetFormInputText(),
      'anio'                => new sfWidgetFormInputText(),
      'serie'               => new sfWidgetFormInputText(),
      'telefono'            => new sfWidgetFormInputText(),
      'seguro'              => new sfWidgetFormInputText(),
      'seguro_telefono'     => new sfWidgetFormInputText(),
      'seguro_vencimiento'  => new sfWidgetFormDate(),
      'status'              => new sfWidgetFormInputText(),
      'is_franquicia'       => new sfWidgetFormInputText(),
      'fecha_verificacion'  => new sfWidgetFormDate(),
      'fecha_mantenimiento' => new sfWidgetFormDate(),
      'alerta_kilometraje'  => new sfWidgetFormInputText(),
      'created_at'          => new sfWidgetFormDateTime(),
      'updated_at'          => new sfWidgetFormDateTime(),
      'categoria_list'      => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Categoria')),
    ));

    $this->setValidators(array(
      'id'                  => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'empresa_id'          => new sfValidatorInteger(),
      'area_id'             => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Area'))),
      'operador_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Operador'), 'required' => false)),
      'ayudante_id'         => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Ayudante'), 'required' => false)),
      'nombre'              => new sfValidatorString(array('max_length' => 100)),
      'placa'               => new sfValidatorString(array('max_length' => 100)),
      'marca'               => new sfValidatorString(array('max_length' => 100)),
      'modelo'              => new sfValidatorString(array('max_length' => 100)),
      'anio'                => new sfValidatorString(array('max_length' => 25)),
      'serie'               => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'telefono'            => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'seguro'              => new sfValidatorString(array('max_length' => 100, 'required' => false)),
      'seguro_telefono'     => new sfValidatorString(array('max_length' => 25, 'required' => false)),
      'seguro_vencimiento'  => new sfValidatorDate(array('required' => false)),
      'status'              => new sfValidatorInteger(array('required' => false)),
      'is_franquicia'       => new sfValidatorInteger(),
      'fecha_verificacion'  => new sfValidatorDate(array('required' => false)),
      'fecha_mantenimiento' => new sfValidatorDate(array('required' => false)),
      'alerta_kilometraje'  => new sfValidatorInteger(array('required' => false)),
      'created_at'          => new sfValidatorDateTime(),
      'updated_at'          => new sfValidatorDateTime(),
      'categoria_list'      => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'Categoria', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('camioneta[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'Camioneta';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['categoria_list']))
    {
      $this->setDefault('categoria_list', $this->object->Categoria->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveCategoriaList($con);

    parent::doSave($con);
  }

  public function saveCategoriaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['categoria_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->Categoria->getPrimaryKeys();
    $values = $this->getValue('categoria_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('Categoria', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('Categoria', array_values($link));
    }
  }

}
